/*
 * libqueue
 * Copyright (c) 2007 PRAV
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* 1.0.0: Ronaldo - Funcao dequeue recebe ponteiro da fila a ser consultada
   0.1.3: Iuri - adaptacao com inclusao de novas funcoes
   0.1.2: Iuri - comentarios e codigos
   0.1.1: Gaspare - release inicial
 */

#ifndef __LIB_EVENT_QUEUE__
#define __LIB_EVENT_QUEUE__

#include "stdlib.h"
#include <unistd.h>
#include <stdint.h>

#define QUEUE_BUFFER_SIZE           100
#define QUEUE_BUFFER_TOTAL_NUMBER   20

#define E_QUEUE_ERROR                    200
#define E_UNDEFINED_QUEUE                201
#define E_EMPTY_QUEUE                    202
#define E_END_OF_QUEUE                   203
#define E_QUEUE_INVALID_CONSUMER         204
#define E_QUEUE_FREE_NEEDED              205
#define E_QUEUE_MAX_CONSUMERS_REACHED    206
#define E_QUEUE_MAX_LENGTH_REACHED       207
#define E_QUEUE_INSUFFICIENT_MEMORY      208
#define E_OK                             0

typedef struct davint_event_queue_t {
  //int length;                              // tamanho da queue (numero de elementos)
  //uint32_t size;                           // memoria total ocupada pela queue, em bytes
  uint8_t  next_buffer;
  uint8_t  last_buffer;
  uint8_t  queue_buffer[QUEUE_BUFFER_TOTAL_NUMBER][QUEUE_BUFFER_SIZE];
  uint32_t queue_buffer_ts[QUEUE_BUFFER_TOTAL_NUMBER];
  uint32_t queue_buffer_size[QUEUE_BUFFER_TOTAL_NUMBER];

  /*uint8_t*  internal_queue_buffer[2];
  uint32_t internal_queue_size_buffer[2];
  uint32_t internal_timestamp_buffer[2];
  uint8_t  actual_internal_buffer;
  uint32_t actual_internal_position;*/
} davint_event_queue_t;


// definidos internamente na DLL e nao exportados!
// evita uso indevido...
//typedef struct queue_t queue_t;
//typedef struct consumer_t consumer_t;


// inicializa uma nova fila de dados
// Retorno:   ponteiro para a nova fila criada, em caso de sucesso;
//            NULL, caso contrario
extern davint_event_queue_t *davint_event_init_queue();

// coloca um buffer na fila
// informar em "buffersize" o tamanho do buffer
// NAO DESALOCAR O BUFFER! pois ele nao copia, apenas armazena...
// Retorno:   E_OK, em caso de sucesso
//            E_UNDEFINED_QUEUE, caso a fila nao tenha sido inicializada
//            E_MEMORY_OVERFLOW, caso nao haja memoria para a operacao
extern int davint_event_enqueue(davint_event_queue_t *q, uint8_t *buf, uint32_t buffersize, uint32_t timestamp);


// retira um buffer da fila
// informar apenas o usuario e a variavel que ira receber o buffer
// seta buf com o endereco do buffer retirado da fila e t com o tamanho
// NAO DESALOCAR O BUFFER! em vez disso, apos o uso, chame queue_free
// para liberar o buffer
// Retorno:   E_OK, em caso de sucesso
//            E_UNDEFINED_QUEUE, caso a fila nao tenha sido inicializada
//            E_EMPTY_QUEUE, caso a fila esteja vazia
extern int davint_event_dequeue(davint_event_queue_t *q, uint8_t **buf, uint32_t *buffersize_p, uint32_t *timestamp);

#endif
